package com.das.mechanic_base.adapter.maintain;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.AloneTitleBean;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenSi on 2018/3/13.  tain_detail_fragment
 */

public class X3AloneAdapter extends RecyclerView.Adapter<X3AloneAdapter.AloneContentHolder>{

    private List<AloneTitleBean> aloneTitleBeans;

    private Context mContext;

    private long workBaseId;

    public X3AloneAdapter(Context mContext, long workBaseId) {
        this.mContext = mContext;
        this.workBaseId = workBaseId;
        aloneTitleBeans = new ArrayList<>();
    }

    public void ChangeAloneTitle(List<AloneTitleBean> aloneTitleBeans) {
        this.aloneTitleBeans = aloneTitleBeans;
        notifyDataSetChanged();
    }

    @Override
    public AloneContentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.x3_sive_alone_item,parent,false);
        AloneContentHolder viewHolder = new AloneContentHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final AloneContentHolder holder, int position) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        if (position == aloneTitleBeans.size() - 1) {
            // 设置top 0
            layoutParams.bottomMargin = X3ScreenUtils.dipToPx(15,mContext);
            holder.v_line.setVisibility(View.INVISIBLE);
        }else {
            layoutParams.bottomMargin = 0;
            holder.v_line.setVisibility(View.VISIBLE);
        }
        holder.itemView.setLayoutParams(layoutParams);

        int alreadyAsk = (int) SpHelper.getData(aloneTitleBeans.get(position).getMgtCreate() + workBaseId + aloneTitleBeans.get(position).getQuestionToSystemId() + "AnswerAmount", 0);

        int questionAmount = (int) aloneTitleBeans.get(position).getQuestionAmount();

        boolean isRed = (boolean) SpHelper.getData(workBaseId + aloneTitleBeans.get(position).getQuestionToSystemId() + "ISRED", false);
        if (alreadyAsk >= questionAmount && aloneTitleBeans.get(position).isIfSubmit() && !isRed) {
            alreadyAsk = questionAmount;
            holder.iv_point.setImageResource(R.mipmap.x3_alone_finish);
        } else if (aloneTitleBeans.get(position).isIfSubmit()){
            alreadyAsk = questionAmount;
            holder.iv_point.setImageResource(R.mipmap.x3_alone_finish);
        }else {
            holder.iv_point.setImageResource(R.mipmap.x3_alone_not_finish);
        }

        if (questionAmount == 1) {
            holder.tv_num.setText(String.format(mContext.getString(R.string.x3_main_tain_num_string),alreadyAsk + "/" + aloneTitleBeans.get(position).getQuestionAmount()));

        }else {
            holder.tv_num.setText(String.format(mContext.getString(R.string.x3_main_tain_nums_string),alreadyAsk + "/" + aloneTitleBeans.get(position).getQuestionAmount()));

        }

        holder.tv_title.setText((position + 1) + "." + aloneTitleBeans.get(position).getToSystemDesc() + "");

        holder.itemView.setOnClickListener(view -> {
            if (iOnClickItem != null) {
                iOnClickItem.iOnItemClickListener(position,
                        aloneTitleBeans.get(position).getToSystemDesc(),
                        aloneTitleBeans.get(position).getQuestionToSystemId(),aloneTitleBeans.get(position).getId(),aloneTitleBeans.get(position).getMgtCreate());
            }
        });
    }

    private IOnItemClick iOnClickItem;

    public void setIOnClickItem(IOnItemClick iOnClickItem) {
        this.iOnClickItem = iOnClickItem;
    }

    public interface IOnItemClick {
        void iOnItemClickListener(int position,String title,long questionId,long toSystemId,String mgtCreate);
    }

    @Override
    public int getItemCount() {
        return aloneTitleBeans.size();
    }

    class AloneContentHolder extends RecyclerView.ViewHolder{
        private TextView tv_title;
        private ImageView iv_point;
        private TextView tv_num;
        private View v_line;
        public AloneContentHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            iv_point = itemView.findViewById(R.id.iv_point);
            tv_num = itemView.findViewById(R.id.tv_num);
            v_line = itemView.findViewById(R.id.v_line);
        }
    }
}

